
import {BaseURL} from '../../app.js';


//获取应用实例
const app = getApp()

Page({

  data: {
    phone: '',
    openId: '',
    // hiddenmodalput: false,
    //可以通过hidden是否掩藏弹出框的属性，来指定那个弹出框 
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false
  },
  isPoneAvailable(pone) {
    var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (!myreg.test(pone)) {
      return false;
    } else {
      return true;
    }
  },
  doorControlScence() {

    if (!this.data.phone) {
      this.setData({
        hiddenmodalput: false
      })
      return;
    }

    var phoneValue = wx.getStorageSync('phone');

    if (this.data.phone === phoneValue ) {

      wx.navigateTo({
        url: '../door-control/door-control?phone=' + this.data.phone
      })

    } else {
      wx.showToast({
        title: "不是本人"
      })
    }

  },

  playCard: function() {

    if (!this.data.phone) {
      this.setData({
        hiddenmodalput: false
      })
      return;
    }

    // this.data.phone = wx.getStorageSync('phone');

    var phoneValue = wx.getStorageSync('phone');
    if (this.data.phone === phoneValue) {
      wx.navigateTo({
        url: '../playcard/playcard?phone=' + this.data.phone
      })
    } else {
      wx.showToast({
        title: "不是本人"
      })
    }
    
  },

  modalinputWin: function(e) {
    this.setData({
      hiddenmodalput: !this.data.hiddenmodalput
    })
  },
  //事件处理函数
  //点击按钮弹出指定的hiddenmodalput弹出框  
  modalinput: function(e) {
    if (e.detail.value) {
      this.setData({
        phone: e.detail.value
      })
     
      if (!this.isPoneAvailable(this.data.phone)) {
       
        wx.showModal({
          title: '提示',
          content: '手机号输入错误',
          showCancel: false,
          success: function (res) {

            wx.redirectTo({
              url: '../index/index'
            })
          }
        })
      }

      wx.setStorageSync("phone", this.data.phone);
      this.logIn();

    }
    
    // else {
    //   wx.clearStorageSync();
    // }
  },
  //取消按钮  
  cancel: function() {
    this.setData({
      hiddenmodalput: true
    });
  },
  //确认  
  confirm: function() {
    this.setData({
      hiddenmodalput: true
    })
    // this.modalinput();


    console.log("==this.data.phone的值是====", this.data.phone);
    console.log("==this.data.openId的值是====", this.data.openId);

  },
  logIn() {
    let that = this;
    let phoneValue = that.data.phone;
    let openIdValue = that.data.openId;

    console.log("==logIn中.phone的值是====", phoneValue);
    console.log("==logIn中.openId的值是====", that.data.openId);

    wx.request({
      method: 'POST', 
      url: `${BaseURL}/safe/attendance/logIn`,
      // url: "http://172.16.16.77:8180/safe/attendance/logIn",

      data: {
        phone: phoneValue,
        openId: that.data.openId
      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {

        var aa = res.data;
        console.log("=====", res);
        if (res.data.code == 0) {
          console.log("==res.data.code===", res.data.code);

          switch (res.data.data.dataCode) {
            case 0:
              console.log("该用户不存在");
              wx.showToast({
                title: res.data.data.dataMessage
              })
              break;
            case 1:
              console.log("登陆成功");
              wx.showToast({
                title: res.data.data.dataMessage
              })
              break;
            case 2:
              console.log("需要验证");
              wx.navigateTo({
                url: '../video-door/video-door'
              })
              break;
            case 3:
              console.log("需要注册");
              wx.navigateTo({
                url: '../register/register?phone=' + wx.getStorageSync("phone")
              })
              break;
            default: 
              wx.showToast({
                title: "default"
              })
              console.log("需要验证");
          }

        } else {
          wx.showToast({
            title: res.msg,
          })
          console.log("=====", res.msg);
        }
      },
      fail: function() {
        wx.showModal({
          title: '提示',
          content: '网络失败',
          showCancel: false,
          success: function(res) {

            wx.redirectTo({
              url: '../index/index'
            })
          }
        })
      }
    })
  },
  onLoad: function(options) {

    if (options.hiddenmodalput){

      this.setData({
        hiddenmodalput: options.hiddenmodalput,
        phone:options.phone
      })

    }

    var user = wx.getStorageSync('user');

    this.setData({
      openId: user.openid
    })
  }
})